const db = require("../config/config");
module.exports = {
  //获取所有订单信息
  getOrderInfo (req, res) {
    db.dbMysql('SELECT * from orderView order by orderId asc', [], function (err, data) {
      if (data) {
        res.send({
          code: 200,
          data: data,
          msg: '购物车信息查询成功'
        })
      } else {
        res.send({
          code: 500,
          msg: '服务器错误'
        })
      }
    });
  },
  //根据条件查询  /order/searchOrderByKeys
  searchOrderByKeys (req, res) {
    console.log(req.body);
    let a = ``
    let b = ''
    for (key in req.body) {
      if (key == 'keywords') {
        a += ` user_phone like '%${req.body[key]}%' and`
      }
      if (key == 'orderState') {
        a += ` orState=${req.body[key]}  and`
      }
      if (key == 'startTime') {
        if (a == '') {
          b = ` orStartTime between '${req.body[key]}' and `
        } else {
          b = `and orStartTime between '${req.body[key]}' and `
        }
      }
      if (key == 'endTime') {
        b += `'${req.body[key]}'`
      }
    }
    let sql = 'SELECT * from orderView where' + a.slice(0, -3) + b
    db.dbMysql(sql, [], function (err, data) {
      if (data) {
        res.send({
          code: 200,
          data: data,
          msg: '查询成功'
        })
      } else {
        res.send({
          code: 500,
          msg: '服务器错误'
        })
      }
    });
  },
  //批量修改状态，或修改一条状态
  changeManyState (req, res) {
    let { idArr, orState } = req.body
    let a = ""
    let b = '('
    idArr.forEach(item => {
      let str = `WHEN ${item} THEN ${orState} `
      b += item + ","
      a += str
    })
    b = b.slice(0, b.length - 1) + ')'
    let sql = `UPDATE orderinfo SET orState = CASE orderId ${a} END WHERE orderId  IN ${b} ;`
    db.dbMysql(sql, [], function (err, data) {
      if (data) {
        res.send({
          code: 200,
          msg: '修改成功'
        })
      } else {
        res.send({
          code: 500,
          msg: '服务器错误'
        })
      }
    });
  },
  //删除多条订单信息
  delManyOrderInfo (req, res) {
    let { idArr } = req.body
    console.log('req.body', req.body);
    let str = 'DELETE FROM orderinfo WHERE orderId in ('
    idArr.forEach(item => {
      str += item + ','
    })
    let sql = str.slice(0, str.length - 1) + ")"
    console.log('sql', sql);
    db.dbMysql(sql, [], function (err, data) {
      if (data) {
        res.send({
          code: 200,
          msg: '删除成功'
        })
      } else {
        res.send({
          code: 500,
          msg: '服务器错误'
        })
      }
    })
  },
  //根据订单ID获取订单信息
  getOrderById (req, res) {
    console.log('req', req.body);
    db.dbMysql('SELECT * from orderinfo where orderId=?', [req.body.id], function (err, data) {
      if (data) {
        res.send({
          code: 200,
          data: data,
          msg: '查询成功'
        })
      } else {
        res.send({
          code: 500,
          msg: '服务器错误'
        })
      }
    });
  },
  //根据ID获取地址数据
  getAddressById (req, res) {
    console.log('req', req.body);
    db.dbMysql('SELECT * from shop_address where address_id=?', [req.body.id], function (err, data) {
      if (data) {
        res.send({
          code: 200,
          data: data,
          msg: '查询成功'
        })
      } else {
        res.send({
          code: 500,
          msg: '服务器错误'
        })
      }
    });
  },
  //根据ID获取商品数据
  getProductById (req, res) {
    console.log('req', req.body);
    db.dbMysql('SELECT * from product where productID=?', [req.body.id], function (err, data) {
      if (data) {
        res.send({
          code: 200,
          data: data,
          msg: '查询成功'
        })
      } else {
        res.send({
          code: 500,
          msg: '服务器错误'
        })
      }
    });
  },
  //查一级标签分类
  getClassifyOne (req, res) {
    db.dbMysql('select * from classify where classifyClass=1', [], function (err, data) {
      if (data) {
        res.send({
          code: 200,
          data: data,
          msg: '一级菜单查询成功'
        })
      } else {
        res.send({
          code: 500,
          msg: '服务器错误'
        })
      }
    });
  },
  //获取所有上架标签
  getUpClassifyInfo (req, res) {
    db.dbMysql('select * from classify where classifyType=1', [], function (err, data) {
      if (data) {
        res.send({
          code: 200,
          data: data,
          msg: '查询成功'
        })
      } else {
        res.send({
          code: 500,
          msg: '服务器错误'
        })
      }
    });
  },
  //查询某个一级标签下的子标签
  getClassifyChild (req, res) {
    db.dbMysql('select * from classify where classifyParentID=?', [req.body.id], function (err, data) {
      if (data) {
        res.send({
          code: 200,
          data: data,
          msg: '查询成功'
        })
      } else {
        res.send({
          code: 500,
          msg: '服务器错误'
        })
      }
    });
  },
  //查询某个二级标签下，已上架的商品
  getUpProductByLabelId (req, res) {
    console.log('req.body', req.body);
    let { labelChild } = req.body
    db.dbMysql('select * from product where productClassify2=? and productGrounding=1', [labelChild], function (err, data) {
      if (data) {
        res.send({
          code: 200,
          data: data,
          msg: '查询成功'
        })
      } else {
        res.send({
          code: 500,
          msg: '服务器错误'
        })
      }
    });
  },
  //根据订单id 查询对应的售后信息
  getRefundByOrderId (req, res) {
    db.dbMysql('SELECT * FROM `refund` WHERE key_order_id=?', [req.body.id], function (err, data) {
      if (data) {
        res.send({
          code: 200,
          data: data,
          msg: '查询成功'
        })
      } else {
        res.send({
          code: 500,
          msg: '服务器错误'
        })
      }
    });
  },
  //添加订单信息
  addOrder (req, res) {
    let { orNum, orBuyerId, orAddressId, productID, orPrice, orStartTime } = req.body
    orStartTime = db.formatDateTime(new Date(orStartTime))

    db.dbMysql('INSERT INTO orderinfo (orNum,orPrice,orStartTime,orBuyerId,orAddressId,orProductId) VALUES (?,?,?,?,?,?) ', [orNum, orPrice, orStartTime, orBuyerId, orAddressId, productID], function (err, data) {
      if (data) {
        res.send({
          code: 200,
          msg: '添加成功'
        })
      } else {
        res.send({
          code: 500,
          msg: '服务器错误'
        })
      }
    });
  }
}


